package com.climaxi.core.ui;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.Log;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URL;

import com.climaxi.core.ClimaxiLog;
import com.climaxi.core.Contants;

public class FileCli {

	public static boolean File_IsAvailable_SDcard(String name)
	{
		File f = new File(Contants.LINK_DATA_SDCARD + File.separator + name);
		if (f.exists())
		{
			ClimaxiLog.Trace("has file on SD card");
			return true;
		
		}
		return false;
	}
	
	public static boolean checkFolderDataInSDCard(String name)
	{
		File f;
		if(name.equals(""))
			f = new File(Contants.LINK_DATA_SDCARD);
		else
			f = new File(Contants.LINK_DATA_SDCARD + File.separator + name);
		
		return (f.exists() && f.isDirectory());
	}
	
	public static boolean createFolderInSDCard(String name)
	{
		File f;
		if(name.equals(""))
			f = new File(Contants.LINK_DATA_SDCARD);
		else
			f = new File(Contants.LINK_DATA_SDCARD + File.separator + name);
		
		if(!f.isDirectory())
		{
			return f.mkdir();
		}
		
		return false;
	}

	public static String getFileNameImage(String link)
	{
		String fileName = "";
		if(!link.equals(""))
		{
			String txt = TextSplit_Get(link, "//", 1, 0);
			fileName = TextSplit_Get(txt, "/", 0, 1);
			ClimaxiLog.Trace(fileName);
		}
		return fileName;
	}
	
	public static Bitmap LoadImage_FromSDcard(String name)
	{
		String filename = Contants.LINK_DATA_SDCARD + File.separator + name;
		File f = new File(filename);
		if(f.exists())
		{
			BitmapFactory.Options options = new BitmapFactory.Options();			
			options.inDither = false;
			options.inJustDecodeBounds = false;
			options.inPurgeable = true;
			
			FileInputStream fs;
			try {
				fs = new FileInputStream(f);
				return BitmapFactory.decodeFileDescriptor(fs.getFD(), null, options);
			} catch (FileNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

			//return BitmapFactory.decodeFile(filename, options);	
		}
		return null;
	}
	
	public static void SaveImageToSDcard(Bitmap bm, String folder, String fileName)
    {
    	FileOutputStream outStream = null;
    	
    	String link = Contants.LINK_DATA_SDCARD + File.separator + folder;
    	
    	File f = new File(link); 
	    if(!f.isDirectory())
	    {
	      f.mkdir();
	    }
    	
		File file = new File(link, fileName);
		
		try
		{
			outStream = new FileOutputStream(file);
			if(fileName.indexOf("png") >= 0)
				bm.compress(Bitmap.CompressFormat.PNG, 100, outStream);
			else if(fileName.indexOf("jpg") >= 0 || fileName.indexOf("jpge") >= 0)
				bm.compress(Bitmap.CompressFormat.JPEG, 100, outStream);
			else
				return;
			outStream.flush();
			outStream.close();	
			//ClimaxiLog.Trace("save sucess");
		}
		catch (FileNotFoundException e) 
		{
			//ClimaxiLog.Trace("save fail");
			e.printStackTrace();
		}
		catch (IOException e) 
		{			
			//ClimaxiLog.Trace("save fail");
			e.printStackTrace();
		}
    }
	
	public static Bitmap LoadImageFormUrl(String url)
	{
		Bitmap bitmap = null;
		InputStream in = null;
		BufferedOutputStream out = null;
		try {
			in = new BufferedInputStream(new URL(url).openStream(), Contants.IO_BUFFER_SIZE);
			final ByteArrayOutputStream dataStream = new ByteArrayOutputStream();
			out = new BufferedOutputStream(dataStream, Contants.IO_BUFFER_SIZE);
			copy(in, out);
			out.flush();
			final byte[] data = dataStream.toByteArray();
			bitmap = BitmapFactory.decodeByteArray(data, 0, data.length);
			return bitmap;

		} catch (Exception ex){

		}
		return null;        
	}
	
	public static String TextSplit_Get(String text,String arg,int position,int getEnd)
    {
    	String []temp;
    	temp = text.split(arg);
    	if(getEnd > 0)
    	{
    		return temp[temp.length - getEnd];
    	}
    	return temp[position];
    }
	
	private static void copy(InputStream in, OutputStream out) throws IOException 
	{
	    byte[] b = new byte[Contants.IO_BUFFER_SIZE];
	    int read;
	    while ((read = in.read(b)) != -1) 
	    {
	        out.write(b, 0, read);
	    }
	}
}
